package com.service;

import com.bean.base.UserInfo;
import com.bean.other.MetaInfo;
import com.bean.wx.UserInfoWx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.UserInfoMapper;
import com.mapper.repository.UserInfoRepository;
import com.mapper.repositorywx.UserInfoWxRepository;
import com.service.impl.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public String insert(UserInfo userInfo) {
        int count = userInfoRepository.countByUsername(userInfo.getUsername());
        if(count>0){
            return "当前用户名已存在，请重新输入";
        }
        userInfo.setId(UUID.randomUUID().toString());
        userInfo.setCreatetime(LocalDateTime.now());
        userInfoRepository.save(userInfo);
        return "新增成功";
    }

    @Override
    public String init(UserInfo userInfo) {
        int count = userInfoRepository.countByUsername(userInfo.getUsername());
        if(count>0){
            return "当前用户名已存在，请重新输入";
        }
        userInfo.setId(userInfo.getCreator());
        userInfo.setCreatetime(LocalDateTime.now());
        userInfoRepository.save(userInfo);
        return "初始化成功";
    }

    @Override
    public String update(UserInfo userInfo) {
        userInfo.setUpdatetime(LocalDateTime.now());
        userInfoRepository.save(userInfo);
        return "更新成功";
    }

    @Override
    public String delete(UserInfo userInfo) {
        userInfoRepository.delete(userInfo);
        return "删除成功";
    }

    @Override
    public PageInfo<UserInfo> select(UserInfo userInfo, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize(),"createtime");
        List<UserInfo> list=userInfoMapper.select(userInfo);
        PageInfo<UserInfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public UserInfo get(String id) {
        Optional<UserInfo> optional=userInfoRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Override
    public UserInfo login(UserInfo userInfo) {
        return userInfoMapper.selectOne(userInfo);
    }

    @Autowired
    private UserInfoWxRepository userInfoWxRepository;

    @Override
    public UserInfoWx login(UserInfoWx userInfoWx) {
        if(userInfoWx.getOpenid()==null||userInfoWx.getOpenid().equals("")){
            //return null;
        }else{
            userInfoWx = userInfoWxRepository.findByOpenid(userInfoWx.getOpenid());
            return userInfoWx;
        }
        return null;
    }
}
